package com.mall.consumer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.mall.pojo.goods.Goods;
import com.mall.pojo.goods.Sku;
import com.mall.pojo.goods.Spu;
import com.mall.service.goods.CategoryService;
import com.mall.service.goods.SpuService;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQPushConsumerLifecycleListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;


import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
@RocketMQMessageListener(topic = "goods_es_sms",consumerGroup = "goods_consumer")
public class SkuInsertEsConsumer implements RocketMQListener<String>, RocketMQPushConsumerLifecycleListener {
    private static Logger log = LoggerFactory.getLogger(SkuInsertEsConsumer.class);
    @Autowired
    private TemplateEngine templateEngine;

    @Autowired
    private SpuService spuService;

    @Autowired
    private CategoryService categoryService;

    @Value("d:/static_images")
    private String pagePath;

    @Override
    public void onMessage(String s) {
        Map<String,String> sms = JSON.parseObject(s,Map.class);
        String sku_id = sms.get("sku_id");
        String spu_id = sms.get("spu_id");
        log.info("[nxmall_service_goods][SkuInsertEsConsumer]"+sms);
        log.info("[sku_id]"+sku_id+"[spu_id]"+spu_id);
        createPage(spu_id);
    }

    private void createPage(String spu_id) {
        log.info("[nxmall_service_goods][SkuInsertEsConsumer][insertEsGoods][生成商品静态页开始！]");
        //查询商品信息
        Goods goods = spuService.findGoodsById(spu_id);
        //获取spu信息
        Spu spu = goods.getSpu();
        //获取skulb
        List<Sku> skuList = goods.getSkuList();
        //查询商品分类
        List<String> categoryList=new ArrayList<String>();
        categoryList.add(categoryService.findById(spu.getCategory1Id()).getName());//一级分类
        categoryList.add(categoryService.findById(spu.getCategory2Id()).getName());//二级分类
        categoryList.add(categoryService.findById(spu.getCategory3Id()).getName());//三级分类

        //生成sku地址列表
        Map urlMap =new HashMap();
        for (Sku sku : skuList) {
            //todo 意义何在
            //对规格json字符串进行排序
            String specJson = JSON.toJSONString(JSON.parseObject(sku.getSpec()), SerializerFeature.SortField.MapSortField);
            urlMap.put(specJson,sku.getId()+".html");
        }

        //创建页面(每个sku一个页面)
        for (Sku sku : skuList) {
            log.info("sku{}:",sku);
            log.info("spu{}:",spu);
            //1.上下文
            Context context =new Context();
            //创建数据模型
            Map<String,Object> dataModel=new HashMap<>();
            dataModel.put("spu",spu);
            dataModel.put("sku",sku);

            dataModel.put("categoryList",categoryList);//商品分类面包屑

            if(null != sku.getImages() && !"".equals(sku.getImages())){
                dataModel.put("skuImages",  sku.getImages().split(","));//SKU图片列表
            }
            if(null != spu.getImages() && !"".equals(spu.getImages())){
                dataModel.put("spuImages",  spu.getImages().split(","));//SPU图片列表
            }

            Map paraItems = JSON.parseObject(spu.getParaItems());//SPU参数列表
            dataModel.put("paraItems", paraItems);
            Map specItems = JSON.parseObject(sku.getSpec());//当前SKU规格
            dataModel.put("specItems", specItems);

//            //规格选择面板
//            Map<String,List> specMap =(Map) JSON.parse(spu.getSpecItems());
//            for (String key : specMap.keySet()) {//循环规格名称
//                List<String> list = specMap.get(key);
//                List<Map> mapList = new ArrayList<>();
//                for (String value : list) {
//                    Map map=new HashMap();
//                    map.put("option",value);
//                    map.put("checked",false);
//                    if (specItems.get(key) !=null && specItems.get(key).equals(value)){
//                        map.put("checked",true);
//                    }
//                    //商品详细页地址
//                    Map spec = JSON.parseObject(sku.getSpec());
//                    spec.put(key,value);
//                    String specJson=  JSON.toJSONString( spec, SerializerFeature.SortField.MapSortField );
//                    map.put("url",urlMap.get(specJson));
//
//                    mapList.add(map);
//                }
//                specMap.put(key,mapList);//用新集合覆盖原集合
//            }
//            dataModel.put("specMap",specMap);//规格面板
            context.setVariables(dataModel);
            //2.准备文件
            File dir = new File(pagePath);
            if (!dir.exists()){
                dir.mkdirs();
            }
            File dest =new File(dir,sku.getId()+".html");
            //3.生成页面
            try {
                PrintWriter writer =new PrintWriter(dest,"UTF-8");
                templateEngine.process("statichtml/item",context,writer);
            }catch (Exception e){
                e.printStackTrace();
            }
            log.info("[nxmall_service_goods][SkuInsertEsConsumer][insertEsGoods][生成商品静态页结束！]");
        }
    }

    @Override
    public void prepareStart(DefaultMQPushConsumer defaultMQPushConsumer) {
        defaultMQPushConsumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET);

    }
}
